import { CHANGE_APP_THEME } from '../actions/sider-action';
import React, { Component } from 'react';
import { Icon } from 'antd';

//state的初始化数据
const initialState = {
    theme: 'light',
    collapsed:false,
    visible:false,
    isFullScreen:false,
    panes:[
        // {
        //     title:<Icon type="home" />,
        //     content:"主页",
        //     key:'0',
        //     closable:false,
        // },
    ],
    activeKey:'0',
    newTabIndex:'1',
    isTableShow:true,
}

//reducer 可以接收state，但是不能修改state
export default function(state=initialState,action){

    //state 表示上次存储的state，action表示用户传递过来的数据
    // console.log(state,action);

    //reducer里只能接收state，不能改变state
    if(action.type === 'chang_theme'){
        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        newState.theme = action.payload ? 'light' : 'dark'
        return newState;
    }

    if(action.type === 'change_collapse'){
        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        newState.collapsed = !action.payload
        return newState;
    }

    if(action.type === 'change_close'){
        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        newState.visible = !action.payload
        return newState;
    }

    if(action.type === 'change_screen'){
        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        if(!action.payload){
            var de = document.documentElement;
            de.requestFullscreen();
        }else{
            var de = document;
            de.webkitCancelFullScreen();
        }
        newState.isFullScreen = !action.payload
        return newState;
    }

    if(action.type === 'add_Pane'){

        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        newState.panes.push({
            title:newState.newTabIndex,
            content:action.content,
            key:newState.newTabIndex++,
            closable:true,
        })
        return newState;
    }

    if(action.type === 'change_active_key'){
        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        newState.activeKey = action.payload;
        return newState;
    }

    if(action.type === 'delete_Pane'){
        //对之前的state做一个深拷贝
        const newState = JSON.parse(JSON.stringify(state));
        const panes = newState.panes.filter(pane => {
            //注意类型的转换，判断
            if(Number(pane.key) !== Number(action.payload)){
        
                return pane;
            }
        });

        newState.panes = panes;
        return newState;
    }

    if(action.type === 'change_table_status'){

        const newState = JSON.parse(JSON.stringify(state));
        newState.isTableShow = !action.payload;
        return newState;
    }

    return state
}